import Calc from "@/utils/calc"
import lodash from "@/utils/lodash"
import { GOODS_TYPE } from "@/config/types"
import {formatAmount, formatHtml} from "@/utils/utils";

/**
 * 过滤 style
 */
export function filterStyles(obj:object={}) {
  const arr = [ null, undefined, '', 'px', 'rpx', '0rpx', false ]
  const styles = {}
  Object.keys(obj).filter(attr => {
    arr.indexOf(obj[attr]) == -1 && (styles[attr] = obj[attr])
  })
  return styles
}


/**
 * 过滤商品信息
 */
export function filterGoodsInfo(goods) {
  //  直推分享金额
  goods.shareAmount = lodash.head(goods.sharedAmount)
  //  节省
  // goods.sharedAmount = Calc.Sub(Number(goods.salePrice), Number(goods.memberPrice))
  //  节省
  goods.saveMoney = Calc.Sub(Number(goods.salePrice), Number(goods.memberPrice))
  //  单位名称
  goods.unitName = goods.unitSnap && goods.unitSnap.name || '件'
  //  是否活动
  goods.isActivity = !!goods.strategy
  //  销售数量
  // goods.salesNumber = Calc.Add(Number(goods.realSalesNumber || 0), Number(goods.baseSalesNumber || 0))
  //  商品详情
  goods.description = goods.description && formatHtml(goods.description)
  //  分享赚
  goods.shareAmount = goods.shareAmount && formatAmount(goods.shareAmount)

  //  商品类型
  switch(goods.type) {
    //  普通商品
    case GOODS_TYPE.REAL:
      //  禁用购买
      goods.disabled = !(goods.isOnSale && goods.stockNumber)
      //  禁用购买描述
      goods.disabledText = goods.isOnSale ? (goods.stockNumber ? '' : '已售罄') : '已下架'
      break
    //  虚拟商品
    case GOODS_TYPE.VIRTUAL:
      //  禁用购买
      goods.disabled = !(goods.isOnSale && goods.stockNumber)
      //  禁用购买描述
      goods.disabledText = goods.isOnSale ? (goods.stockNumber ? '' : '已售罄') : '已下架'
      break
    //  积分商品
    case GOODS_TYPE.SCORE:
      //  禁用购买
      goods.disabled = !(goods.isOnSale && goods.stockNumber)
      //  禁用购买描述
      goods.disabledText = goods.isOnSale ? (goods.stockNumber ? '' : '已售罄') : '已下架'
      break
  }

  const { strategy } = goods
  switch(strategy && goods.saleStrategy) {
    //  秒杀策略
    case GOODS_TYPE.SECKILL:
      //  活动倒计时秒数
      goods.seconds = strategy.status ? strategy.endSeconds : strategy.startSeconds

      //  活动状态[ -1 => 已结束, 0 => 未开始, 1 => 正常 ]
      goods.status = strategy.status
      //  活动状态名称
      goods.statusName = { '-1': '已结束', '0': '未开始' }[strategy.status]

      //  限购数量
      goods.limitNumber = strategy.limitNumber

      //  禁用购买
      goods.disabled = strategy.status < 1
      //  禁用购买描述
      goods.disabledText = { '-1': '已结束', '0': '未开始' }[strategy.status]
      break
  }
  return goods
}
